package com.dkd.applet.handler;

import cn.elegent.ac.ACHandler;
import cn.elegent.ac.ElegentAC;
import cn.elegent.ac.annotation.Topic;
import cn.elegent.pay.ElegentPay;
import cn.elegent.pay.dto.RefundRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dkd.applet.common.constant.VmSystemConstant;
import com.dkd.applet.common.core.ResponseResult;
import com.dkd.applet.common.exception.LogicException;
import com.dkd.applet.config.TopicConfig;
import com.dkd.applet.domain.Channel;
import com.dkd.applet.domain.Order;
import com.dkd.applet.domain.dto.VResultDto;
import com.dkd.applet.service.ChannelService;
import com.dkd.applet.service.OrderService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;

@Topic(TopicConfig.VMS_RESULT_TOPIC)
@Slf4j
public class VendoutResultHandler implements ACHandler<VResultDto> {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ElegentPay elegentPay;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private KafkaTemplate<String,String>  kafkaTemplate;
    @Autowired
    private ElegentAC elegentAC;
    @Override
    public void process(String s, VResultDto vResultDto) throws Exception {
        log.info("接收到出货结果,{}", vResultDto);
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, vResultDto.getOrderNo()));
        if(order==null){
            throw new LogicException("订单不存在");
        }
        if(!order.getStatus().equals(VmSystemConstant.ORDER_STATUS_PAYED)){
           //向前端返回订单状态异常
            //elegentAC.publish(TopicConfig.DKD_SERVER_TOPIC, ResponseResult.error("订单状态异常，订单号为:"+order.getOrderNo()));
            kafkaTemplate.send(TopicConfig.DKD_SERVER_KAFKA_TOPIC, ResponseResult.error("订单状态异常，订单号为:"+order.getOrderNo()).toString());
            throw new LogicException("订单状态异常！");
        }
        if(vResultDto.isSuccess()){
            //订单出货成功
            order.setStatus(VmSystemConstant.ORDER_STATUS_VENDOUT_SUCCESS);
            orderService.updateById( order);
            //扣减库存
            LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Channel::getChannelCode, order.getChannelCode())
                    .eq(Channel::getInnerCode, order.getInnerCode());
            Channel channel = channelService.getOne(queryWrapper);
            channel.setCurrentCapacity(channel.getCurrentCapacity()-1);
            channelService.updateById(channel);
            elegentAC.publish(TopicConfig.DKD_SERVER_TOPIC, ResponseResult.success("出货成功,售出商品的售货机编号为:"+order.getInnerCode()));
        }else{
            //订单出货失败
            order.setStatus(VmSystemConstant.ORDER_STATUS_VENDOUT_FAIL);
            order.setPayStatus(VmSystemConstant.PAY_STATUS_REFUNDING);
            order.setCancelDesc("因机器故障导致出货失败");
            orderService.updateById(order);
            kafkaTemplate.send(TopicConfig.DKD_SERVER_KAFKA_TOPIC, ResponseResult.error("因机器故障导致出货失败,售货机编号为:"+order.getInnerCode()).toString());
            //执行退款流程
            orderService.refund(order.getOrderNo(), order.getPayType());
        }
    }
}